package com.zp.crm.Controller.RoidController;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.zp.crm.entity.Roid.RoidEntity;
import com.zp.crm.service.RoidService.RoidService;
import com.zp.crm.util.Tree.UiTree;

@Controller
@RequestMapping("/roid")
public class RoidController {
	@Resource
	public RoidService roidService;
	
	@RequestMapping(value="/userroid",method=RequestMethod.POST)
	public void UserRoidList(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String code=request.getParameter("code");
		String stuts=request.getParameter("stuts");
		Map<String, Object> map=roidService.UserRoidList(code,stuts);
		try{
			PrintWriter out=response.getWriter();
			String a=(String) map.get("message");
			out.println(a);
		}catch (Exception e) {
				e.printStackTrace();		
		}
		
	}
	
	@RequestMapping(value="/select",method=RequestMethod.POST)
	public void Select(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String pagestr=request.getParameter("page");
		String name=request.getParameter("name");
		int page=1,size=10;
		if(pagestr != null){
			page=Integer.parseInt(pagestr);
		}
		Map<String, Object> map=roidService.select(page,size,name);
		try{
			PrintWriter out=response.getWriter();
			String a=(String) map.get("message");
			out.println(a);
		}catch (Exception e) {
			e.printStackTrace();		
		}
	}
	
	@RequestMapping(value="/RoidMenu",method=RequestMethod.POST)
	public void RoidMenuList(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr!=null){
			id=Integer.parseInt(idstr);
		}
		Map<String, Object> map=roidService.RoidMenuList(id);
		try{
			PrintWriter out=response.getWriter();
			@SuppressWarnings("unchecked")
			List<UiTree> uijson=(List<UiTree>) map.get("message");
			String json=new Gson().toJson(uijson);
			out.println(json);
		}catch (Exception e) {
				e.printStackTrace();		
		}
	}
	
	@RequestMapping(value="/RoidEditSelectMenu",method=RequestMethod.POST)
	public void RoidEditSelectMenu(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr!=null){
			id=Integer.parseInt(idstr);
		}
		Map<String, Object> map=roidService.RoidEditSelectMenu(id);
		try{
			PrintWriter out=response.getWriter();
			@SuppressWarnings("unchecked")
			List<UiTree> uijson=(List<UiTree>) map.get("message");
			String json=new Gson().toJson(uijson);
			out.println(json);
		}catch (Exception e) {
				e.printStackTrace();		
		}
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public void RoidMenuEdit(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String roid=request.getParameter("roid");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr != null){
			id=Integer.parseInt(idstr);
		}
		Map<String, Object> map=roidService.edit(roid,id);
		try{
			PrintWriter out=response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
				e.printStackTrace();		
		}
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void Delete(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr != null){
			id=Integer.parseInt(idstr);
		}
		Map<String, Object> map=roidService.delete(id);
		try{
			PrintWriter out=response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
				e.printStackTrace();		
		}
	}
	
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public void Insert(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String name=request.getParameter("name");
		String roid=request.getParameter("roid");
		String user=request.getParameter("uname");
		RoidEntity entity=new RoidEntity(name, roid, new Date(), user);
		Map<String, Object> map=roidService.insert(entity);
		try{
			PrintWriter out=response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
				e.printStackTrace();		
		}
		
	}
	
}
